namespace ScrumSprintMonitor.TestbenchApp
{
    #region #using Directives

    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;

    using Design;

    using Extensibility;

    using Implementation;

    using Infrastructure;
    using Infrastructure.IoC;

    using Microsoft.Practices.Unity;

    using Testbench.Impl;

    using UI.Wpf;

    using Views.TestbenchView;

    #endregion

    [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses",
        Justification = "The UnityContainerAbstraction will create this class")]
    internal class UnityAppRuntimeModule : StandardModule
    {
        /// <summary>
        ///   Loads the module into the container.
        /// </summary>
        public override void Load()
        {
            Container.RegisterInstance(Container);
            Container.RegisterType(typeof(ILogger<>), typeof(Log4netLogger<>));
            Container.RegisterType(typeof(IAddInHost<>), typeof(AddInHost<>));

            SetupRuntimeBindings();
        }

        private void SetupRuntimeBindings()
        {
            Container.RegisterType<IResourceManager, ResourceManager>(new ContainerControlledLifetimeManager());
            Container.RegisterInstance<IConfigurationManager>(Settings.Default);

			var list = TestAssetLibrary.CreateWorkItems();
            foreach (var adapter in TestAssetLibrary.CreateWorkItemsWithWorkNotEstimated())
            {
                list.Add(adapter);
                list.Add(TestAssetLibrary.CreateWorkItem(list.Count, WorkItemType.UserStory, adapter.AssignedTo, adapter.Team, adapter.State,
                                                         adapter.DoneWork, adapter.RemainingWork));
            }

            Container.RegisterType<IProcessTemplateAdapterProvider, MefProcessTemplateAdapterProvider>(new ContainerControlledLifetimeManager());
            Container.RegisterType<IConnection, TestConnection>();
            Container.RegisterType<IWorkItemStore, TestWorkItemStore>(new ContainerControlledLifetimeManager(),
                                                                      new InjectionConstructor(WritableWorkItem.ConvertToBindingList(list)));
            Container.RegisterType<IAvatarProviderStore, AvatarProviderStore>(new ContainerControlledLifetimeManager());

            Container.RegisterType<ITestbenchViewModel, TestbenchViewModel>(new ContainerControlledLifetimeManager());
        }
    }
}